package from_0_to_50;

import java.util.HashMap;
import java.util.Map;

public class Q020 {
    public static void main(String[] args) {
        String s = "{}}{}{}";
        System.out.println("isValid(s) = " + isValid(s));
    }


    public static boolean isValid(String s) {
        int sLen = s.length();
        char[] stack = new char[sLen+1];
        int top = 1;
        Map<Character, Character> matchMap = new HashMap<>();
        matchMap.put(')', '(');
        matchMap.put('}', '{');
        matchMap.put(']', '[');
         System.out.println(matchMap.toString());
        for(int i = 0; i < sLen; i++){
            System.out.println("i = " + i);
            char bracket = s.charAt(i);
            if(matchMap.get(bracket) == null){
                stack[top++] = bracket;
            }else{
                if(matchMap.get(bracket) == stack[top-1]){
                    top--;
                }else{
                    stack[top++] = bracket;
                    break;
                }
            }
        }
        return top == 1 ? true : false;
    }
}
